import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import {uSelectDevMsgAndStandRange} from '@/api/setting'
import {addressJoin} from '@/utils/common'

class Standardconfig extends ChildrenPageUtils {
  @observable pageDataSource: Setting.StandardDataSource | undefined = undefined
  @computed get getPageDataSource() {
    return this.pageDataSource
  }

  @observable address: string = ''
  @computed get getAddress() {
    return this.address
  }

  @action.bound
  updatePageDataSource(userId: number) {
    if (this.pageQuery.devId) {
      uSelectDevMsgAndStandRange({
        devId: this.pageQuery.devId,
        userId: userId
      })
        .then(res => {
          this.pageDataSource = res.data
          let addr = addressJoin({
            province: res.data.province,
            city: res.data.city,
            district: res.data.district
          })
          this.address = addr || ''
        })
        .catch(err => {
          this.pageDataSource = undefined
          this.address = ''
        })
    }
  }
}
export const standardConfigStore = createContext(new Standardconfig())
